# coding:utf-8
__author__ = '啦拉嘿'
__date__ = '2020/3/7 16:16'
import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_orm.settings")
django.setup()
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo, showerror

from connect_db.models import Category


class Setting:
    def __init__(self, master):
        self.__root = tk.Toplevel(master)
        self.__entry_ttk_frame = ttk.Labelframe(self.__root, text="添加计时种类")
        self.__entry = tk.Entry(self.__entry_ttk_frame) # , text=self.__entry_value.get()
        self.__entry_button = ttk.Button(self.__entry_ttk_frame, text="提交", command=self.__commit_entry)

    def __commit_entry(self):
        category = Category()
        category.category = self.__entry.get()
        try:
            category.save()
        except Exception as e:
            showerror("Error", e)
        self.__entry.delete(0, tk.END)
        showinfo("添加成功", "成功添加" + category.category + "到数据库！")

    def display(self):
        self.__root.title("设置")
        self.__entry_ttk_frame.pack(side=tk.TOP, padx=10, pady=10)
        self.__entry.pack(side=tk.LEFT, padx=10, pady=10)
        self.__entry_button.pack(side=tk.LEFT, padx=10, pady=10)


if __name__ == '__main__':
    root = tk.Tk()
    setting = Setting(root)
    setting.display()
    root.mainloop()
